package com.gnerv.cms.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.SessionListener;
import org.springframework.stereotype.Component;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * shiroSession监听器
 *
 * @author zhang
 * @date 2019/12/3
 **/
@Slf4j
@Component
public class ShiroSessionListener implements SessionListener {

    private static AtomicInteger sessionCount = new AtomicInteger(0);

    @Override
    public void onStart(Session session) {
        sessionCount.incrementAndGet();
        log.info("会话数加一， 当前会话数：" + sessionCount);
    }

    @Override
    public void onStop(Session session) {
        sessionCount.decrementAndGet();
        log.info("会话数减一， 当前会话数：" + sessionCount);
    }

    @Override
    public void onExpiration(Session session) {
        sessionCount.decrementAndGet();
        log.info("会话数减一， 当前会话数：" + sessionCount);
    }

    public AtomicInteger getSessionCount() {
        log.info("当前会话数" + sessionCount);
        return sessionCount;
    }
}
